package com.woniuxy.exam.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.commons.dto.ExamDto;
import com.woniuxy.commons.dto.QuestionDto;
import com.woniuxy.commons.entity.*;
import com.woniuxy.commons.service.RemoteUserService;
import com.woniuxy.commons.utils.TimeUtils;
import com.woniuxy.exam.service.ExamService;
import com.woniuxy.exam.service.ExamTypeService;
import com.woniuxy.exam.utils.TreeUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author zouyang
 * @date 2020/7/23 - 15:34
 */
@RestController
public class ExamController {

    @Autowired
    private ExamService examService;

    @Resource
    private RemoteUserService remoteUserService;

    /**
     * 查询所有的考试
     * @return
     */
    @GetMapping("/exam/all")
    public List<Exam> getAll(){
        List<Exam> exams = examService.selectAll();
        return exams;
    }

    /**
     * 添加考试
     * @param exam
     * @return
     */
    @PostMapping("/exam/add")
    public ResponseEntity add(@RequestBody Exam exam){
        exam.setBeginTime(exam.getDate()[0]);
        exam.setEndTime(exam.getDate()[1]);
        int res = examService.insert(exam);
        if(res!=1){
            return new ResponseEntity(500,"出现异常");
        }
        return new ResponseEntity(200,"success");
    }

    /**
     * 通过id查找某个考试
     * @param id
     * @return
     */
    @GetMapping("/exam/find/{id}")
    public Exam findById(@PathVariable("id") int id){
        return examService.selectByPrimaryKey(id);
    }

    @GetMapping("/exam/allByPage")
    public ResponseEntity getAllByPage(){
        PageHelper.startPage(1,5);
        List<Exam> list = examService.selectAll();
        for (Exam exam : list) {
            exam.setTimingLength(TimeUtils.getTimingLength("yyyy-MM-dd HH:mm:ss","HH时mm分",exam.getBeginTime(),exam.getEndTime()));
        }
        PageInfo<Exam> pageInfo = new PageInfo<>(list,10);
        return new ResponseEntity(200,"success",pageInfo);
    }

    @PostMapping("/exam/caps")
    public ResponseEntity conditionAndPageSearch(@RequestBody ExamDto examDto){
        System.out.println(examDto);
        Exam exam = new Exam();
        BeanUtils.copyProperties(examDto,exam);
        PageHelper.startPage(examDto.getPageNum(),examDto.getPageSize());
        List<Exam> list =  examService.selectByAll(exam);
        for (Exam e : list) {
            e.setTimingLength(TimeUtils.getTimingLength("yyyy-MM-dd HH:mm:ss","HH时mm分",e.getBeginTime(),e.getEndTime()));
        }
        PageInfo pageInfo = new PageInfo(list,10);
        return new ResponseEntity(200,"success",pageInfo);
    }

    @PostMapping("/exam/findExamById")
    public Exam findExamById(@RequestBody Exam exam){
        return examService.selectByPrimaryKey(exam.getId());
    }

    @GetMapping("/exam/getAllUser")
    public ResponseEntity<List<User>> getAllUser(){
        return remoteUserService.selectAllUser();
    }

    @RequestMapping("/exam/update")
    public ResponseEntity update(@RequestBody Exam exam){
        int res = examService.updateByPrimaryKeySelective(exam);
        if(res!=1){
            return new ResponseEntity(500,"出现异常");
        }
        return new ResponseEntity(200,"success");
    }

    @RequestMapping
    public ResponseEntity delete(int id){
        int res = examService.deleteByPrimaryKey(id);
        if(res!=1){
            return new ResponseEntity(500,"出现异常");
        }
        return new ResponseEntity(200,"success");
    }

}
